package com.agileboot.attendance.domain.personnel.model;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.agileboot.attendance.domain.personnel.command.AddPersonnelCommand;
import com.agileboot.attendance.domain.personnel.command.UpdatePersonnelCommand;
import com.agileboot.attendance.domain.personnel.db.AttPersonnelEntity;
import com.agileboot.attendance.domain.personnel.db.AttPersonnelService;
import com.agileboot.attendance.domain.supplier.db.AttSupplierService;
import com.agileboot.common.exception.ApiException;
import com.agileboot.common.exception.error.ErrorCode;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;

/**
 * <p>
 * 人员领域模型
 * </p>
 *
 * @author agileboot
 * @since 2023-03-14
 */
@EqualsAndHashCode(callSuper = true)
@Data
@NoArgsConstructor
public class PersonnelModel extends AttPersonnelEntity {

    private AttPersonnelService personnelService;
    private AttSupplierService supplierService;

    /**
     * 创建领域模型
     * 
     * @param personnelService 人员服务
     * @param supplierService 供应商服务
     */
    public PersonnelModel(AttPersonnelService personnelService, AttSupplierService supplierService) {
        this.personnelService = personnelService;
        this.supplierService = supplierService;
    }

    /**
     * 从实体创建领域模型
     * 
     * @param entity 实体
     * @param personnelService 人员服务
     * @param supplierService 供应商服务
     */
    public PersonnelModel(AttPersonnelEntity entity, AttPersonnelService personnelService, AttSupplierService supplierService) {
        this(personnelService, supplierService);
        if (entity != null) {
            BeanUtil.copyProperties(entity, this);
        }
    }

    /**
     * 加载添加命令数据
     * 
     * @param command 添加命令
     */
    public void loadAddCommand(AddPersonnelCommand command) {
        if (command != null) {
            BeanUtil.copyProperties(command, this, "personnelId");
        }
    }

    /**
     * 加载更新命令数据
     * 
     * @param command 更新命令
     */
    public void loadUpdateCommand(UpdatePersonnelCommand command) {
        if (command != null) {
            loadAddCommand(command);
            this.setPersonnelId(command.getPersonnelId());
        }
    }

    /**
     * 检查人员编码是否唯一
     */
    public void checkPersonnelCodeIsUnique() {
        if (personnelService.isPersonnelCodeDuplicated(getPersonnelCode(), getPersonnelId())) {
            throw new ApiException(ErrorCode.Business.PERSONNEL_CODE_IS_NOT_UNIQUE);
        }
    }

    /**
     * 检查身份证号是否唯一
     */
    public void checkIdNumberIsUnique() {
        if (StrUtil.isNotEmpty(getIdNumber()) && personnelService.isIdNumberDuplicated(getIdNumber(), getPersonnelId())) {
            throw new ApiException(ErrorCode.Business.PERSONNEL_ID_NUMBER_IS_NOT_UNIQUE);
        }
    }

    /**
     * 检查电话号码是否唯一
     */
    public void checkPhoneNumberIsUnique() {
        if (StrUtil.isNotEmpty(getPhoneNumber()) && personnelService.isPhoneNumberDuplicated(getPhoneNumber(), getPersonnelId())) {
            throw new ApiException(ErrorCode.Business.PERSONNEL_PHONE_NUMBER_IS_NOT_UNIQUE);
        }
    }

    /**
     * 检查电子邮箱是否唯一
     */
    public void checkEmailIsUnique() {
        if (StrUtil.isNotEmpty(getEmail()) && personnelService.isEmailDuplicated(getEmail(), getPersonnelId())) {
            throw new ApiException(ErrorCode.Business.PERSONNEL_EMAIL_IS_NOT_UNIQUE);
        }
    }

    /**
     * 检查关联实体是否存在
     */
    public void checkRelatedEntityExist() {
        if (getSupplierId() != null && supplierService.getById(getSupplierId()) == null) {
            throw new ApiException(ErrorCode.Business.SUPPLIER_NOT_FOUND);
        }
    }
} 